<template>
	<view>
		<block v-for="(item, index) in news" :key="index">
			<news-img-right :data="item"  @click="goDetail(item)"></news-img-right>
		</block>
		<view class="uni-tab-bar-loading">
			<uni-load-more :status="loadMoreStatus"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import api from "@/api/index.js"
	import apiConfig from "@/api/config.js"
	import newsImgRight from '@/components/dogner-list-item/news-img-right.vue';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			newsImgRight,
			uniLoadMore,
		},
		data() {
			return {
				news: [],
				page: {
					pageNo: 0
				},
				loadMoreStatus: "more",
			};
		},
		methods: {
			getNews: function() {
				api.cmsService.getNews(++this.page.pageNo).then(res => {
					//data为一个数组，数组第一项为错误信息，第二项为返回数据
					console.log(res.data);
					this.page = res.data;
					if (this.page.pageNo == this.page.last) {
						//最后一页
						this.loadMoreStatus = "noMore"
					} else {
						this.loadMoreStatus = "more"
					}
					if (Array.isArray(res.data.list)) {
						res.data.list.forEach((resItem) => {
							let item = {
								id: resItem.id,
								post_id: resItem.id,
								title: resItem.title,
								author_name: resItem.user.name,
								image_url: apiConfig.CMS_BASE_URL + resItem.imageSrc,
								published_at: resItem.createDate,
								comments_count: resItem.hits,
								content: "",
								category: resItem.category
							}
							this.news.push(item)
						});
					}
				});
			},
			goDetail(detail) {
				uni.navigateTo({
					url: '/pages/news/detail/detail?query=' + encodeURIComponent(JSON.stringify(detail))
				});
			},
			init: function() {
				this.news = [];
				this.page.pageNo = 0;
				// 初始化列表信息
				this.getNews();
			}
		},
		onPullDownRefresh() {
			this.init();
			uni.stopPullDownRefresh()
		},
		onLoad: function() {
			this.init()
		},
		onReachBottom: function() {
			if (this.loadMoreStatus = "more") {
				this.loadMoreStatus = "loading"
				this.getNews();
				console.log("loadmore")
			}
		},
	}
</script>

<style>

</style>
